package com.zhxh.nzpos.sell;

import java.util.ArrayList;
import java.util.Calendar;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.Constants;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.barcode.CaptureActivity;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.model.Sellout;
import com.zhxh.nzpos.utils.RandomStringUtils;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 商品销售中的交易界面
 * 
 * @author wangzt
 *
 */
public class DealActivity extends BaseActivity implements ItemDeleteCallback {

	private ArrayList<Sellout> dealItems;
	private ArrayList<Goods> goodsList;
	private DealListAdapter listAdapter;
	
	private EditText dealOcr;
	
	private GoodsDao goodsDao;
	private String date;
	
	private String orderId;
	
	private String cardId;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sell_deal);
        goodsDao = new GoodsDao(getApplicationContext());
        initViews();
    }
	
	private void initViews() {
		Calendar calendar = Calendar.getInstance();
		date = DateFormat.format("yyyy-MM-dd", calendar).toString();
		orderId = RandomStringUtils.randomNumeric(10);
		cardId = getIntent().getStringExtra(Constants.MEMBER_CARD);
		ListView listView = (ListView) findViewById(R.id.deal_listview);
		dealItems = new ArrayList<Sellout>();
		goodsList = new ArrayList<Goods>();
		
		// TODO 扫描条码添加一项，调用addItem()
		dealOcr = (EditText) findViewById(R.id.deal_ocr_et);
		
		Button scanBtn = (Button) findViewById(R.id.deal_scan_btn);
		scanBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
			//	Intent intent = new Intent(Constants.SCAN_ACTION);
			//	startActivityForResult(intent, Constants.SCAN_REQUEST_CODE);
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), CaptureActivity.class);
				//startActivity(intent);
				startActivityForResult(intent, Constants.SCAN_REQUEST_CODE);
			}
		});
		
		Button addBtn = (Button) findViewById(R.id.deal_add_btn);
		addBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editable ocr = dealOcr.getText();
				if (ocr!= null) {
					String ocrStr = ocr.toString();
					addItem(ocrStr);
				}
			}
		});
		
		listAdapter = new DealListAdapter(getLayoutInflater(), dealItems, goodsList, this);
		listView.setAdapter(listAdapter);
		
		Button settleBtn = (Button) findViewById(R.id.deal_settle_account_btn);
		settleBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (dealItems.size() > 0) {
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(), SettleAccountActivity.class);
					intent.putExtra(Constants.SELL_OUT_EXTRA, dealItems);
					intent.putExtra(Constants.MEMBER_CARD, cardId);
					intent.putExtra(Constants.SELL_NOTE_ID, orderId);
					startActivity(intent);
				//	finish();
				} else {
					ViewUtil.makeToast(getApplicationContext(), "没有出售货物");
				}
			}
		});
/*		Button cancelBtn = (Button) findViewById(R.id.deal_cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});*/
	}

	private void addItem(String goodCode) {
		//如果已经扫描过，则直接加1
		for (Sellout sellout : dealItems) {
			if (goodCode.equals(sellout.getGoodId())) {
				int count = sellout.getCount() + 1;
				sellout.setCount(count);
				if (listAdapter != null) {
					listAdapter.notifyDataSetChanged();
				}
				return;
			}
		}
		
		Goods goods = goodsDao.find(goodCode);
		if (goods != null) {
			Sellout item = new Sellout();
			item.setGoodId(goods.getId());
			item.setGoodsName(goods.getName());
			item.setPrice(goods.getPrice1());
			item.setPonits(goods.getPonits());
			item.setCount(1);
			item.setSellDate(date);
			item.setOpratorId("test1");
			item.setOrderId(orderId);
			dealItems.add(item);
			goodsList.add(goods);
			if (listAdapter != null) {
				listAdapter.notifyDataSetChanged();
			}
		} else {
			ViewUtil.makeToast(getApplication(), "没有该商品的信息");
		}
	}
	
	public void onDelete(int pos) {
		dealItems.remove(pos);
		listAdapter.notifyDataSetChanged();
	}

	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == Constants.SCAN_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				String ocrStr = intent.getStringExtra(Constants.SCAN_RESULT_EXTRA);
				if (!TextUtils.isEmpty(ocrStr)) {
					dealOcr.setText(ocrStr);
				} else {
					Toast.makeText(getApplicationContext(), "请重新扫描！",
							Toast.LENGTH_LONG).show();
				}

			} else if (resultCode == RESULT_CANCELED) {
				Toast.makeText(getApplicationContext(), "请重新扫描！",
						Toast.LENGTH_LONG).show();
			}
		}
	}
}
